using System;
using System.Collections.Specialized;
using System.Web.Mvc;
using RMH.DataAccess;
using RMH.Entities;
using RMH.Services;

namespace RMH.Web.Models.Binders
{
   
    public class StayBinder : IModelBinder
    {
        private IStayService stayService;
        private IPatientService patientService;
        private IRoomService roomService;
        private IPaymentService paymentService;

        public StayBinder(IStayService stayService, IPatientService patientService, IRoomService roomService, IPaymentService paymentService)
        {
            this.stayService = stayService;
            this.paymentService = paymentService;
            this.roomService = roomService;
            this.patientService = patientService;
        }

        public StayBinder()
        {
            stayService = Container.Resolve<IStayService>();
            patientService = Container.Resolve<IPatientService>();
            roomService = Container.Resolve<IRoomService>();
            paymentService = Container.Resolve<IPaymentService>();            
        }

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var form = controllerContext.HttpContext.Request.Form;

            Stay rv = new Stay();
            int containerInt;
            DateTime containerDate;

            if (!String.IsNullOrEmpty(form["Id"]) && Int32.TryParse(form["Id"], out containerInt))
                if (containerInt > 0)
                    rv = stayService.Get(containerInt);
            if (rv == null)
            {
                rv = new Stay();
            }

            if (!String.IsNullOrEmpty(form["RoomId"]) && Int32.TryParse(form["RoomId"], out containerInt))
                rv.Room = roomService.Get(containerInt);

            if (!String.IsNullOrEmpty(form["PatientId"]) && Int32.TryParse(form["PatientId"], out containerInt))
                rv.Patient = patientService.Get(containerInt);

            if (!String.IsNullOrEmpty(form["CheckinDate"]) && DateTime.TryParse(form["CheckinDate"], out containerDate))
                rv.CheckinDate = containerDate;
            else
                rv.CheckinDate = null;
            rv.CheckinBy = form["CheckinBy"];

            if (!String.IsNullOrEmpty(form["CheckoutDate"]) && DateTime.TryParse(form["CheckoutDate"], out containerDate))
                rv.CheckoutDate = containerDate;
            else
                rv.CheckoutDate = null;
            rv.CheckoutBy = form["CheckoutBy"];

            if (!String.IsNullOrEmpty(form["PreauthorizationSentDate"]) && DateTime.TryParse(form["PreauthorizationSentDate"], out containerDate))
                rv.PreauthorizationSentDate = containerDate;
            else
                rv.PreauthorizationSentDate = null;

            if (!String.IsNullOrEmpty(form["PreauthorizationApprovedDate"]) &&
                DateTime.TryParse(form["PreauthorizationApprovedDate"], out containerDate))
                rv.PreauthorizationApprovedDate = containerDate;
            else
                rv.PreauthorizationApprovedDate = null;

            rv.HospitalPhoneNumber = form["HospitalPhoneNumber"];
            rv.MedicaidNumber = form["MedicaidNumber"];
            rv.SocialWorkerName = form["SocialWorkerName"];
            rv.HospitalUnit = form["HospitalUnit"];
            rv.HospitalRoomNumber = form["HospitalRoomNumber"];
            if (!String.IsNullOrEmpty(form["OccupancyType"]))
                rv.Type = (OccupancyType)Int32.Parse(form["OccupancyType"]);
            rv.Payment = getPayment(controllerContext, bindingContext);
            return rv;
        }

        private Payment getPayment(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var form = controllerContext.HttpContext.Request.Form;

            Payment payment;
            if ((String.IsNullOrEmpty(form["Payment.Id"]) == false) && (int.Parse(form["Payment.Id"]) > 0))
                payment = paymentService.Get(Int32.Parse(form["Payment.Id"]));
            else
                payment = new Payment();

            // have a payment?
            if (payment != null)
            {
                if (!String.IsNullOrEmpty(form["Payment.Amount"]))
                    payment.Amount = double.Parse(form["Payment.Amount"]);
                if (!String.IsNullOrEmpty(form["Payment.BillingDate"]))
                    payment.BillingDate = DateTime.Parse(form["Payment.BillingDate"]);

                payment.CheckNumber = form["Payment.CheckNumber"];

                if (!String.IsNullOrEmpty(form["Payment.Method"]))
                    payment.Method = (PaymentMethod)Int32.Parse(form["Payment.Method"]);

                if (!String.IsNullOrEmpty(form["Payment.ReceivedDate"]))
                    payment.ReceivedDate = DateTime.Parse(form["Payment.ReceivedDate"]);

                if (!String.IsNullOrEmpty(form["Payment.Type"]))
                    payment.Type = (PaymentType)Int32.Parse(form["Payment.Type"]);
            }
 
            return payment;
        }
    }
}